package com.woniu.controller;

import com.alibaba.excel.EasyExcel;
import com.woniu.entity.PageData;
import com.woniu.entity.Result;
import com.woniu.entity.dto.CreateOrderDTO;
import com.woniu.entity.dto.QueryOrderDTO;
import com.woniu.entity.vo.DetailOrderVO;
import com.woniu.entity.vo.ListOrderVO;
import com.woniu.service.OrderService;
import com.woniu.utils.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;


@Api(tags = "订单管理类")
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("创建订单")
    @PostMapping("/create")
    public Result createOrder(@RequestBody CreateOrderDTO createOrderDTO){
        Boolean flag = orderService.createOrder(createOrderDTO);
        if(flag){
            return success("订单创建成功");
        }
        return error("订单创建失败，请重试");
    }

    @ApiOperation("结束订单")
    @GetMapping("/toPay/{id}")
    public Result updateOrderToPay(@PathVariable String id){
        int row = orderService.updateOrderToPay(id);
        if(row != 0){
            return success("订单结束成功");
        }
        return error("订单结束失败，请重试");
    }

    @ApiOperation("条件搜索")
    @PostMapping("/list/{currentPage}/{pageSize}")
    public Result listSearch(@PathVariable Integer currentPage,
                             @PathVariable Integer pageSize,
                             @RequestBody(required = false) QueryOrderDTO queryOrderDTO){
        PageData pageData = orderService.listSearch(currentPage,pageSize,queryOrderDTO);
        return toDataResult(pageData);
    }

    @PostMapping("/export")
    @ApiOperation("导出订单")
    public void exportByDTO(@RequestBody(required = false) QueryOrderDTO queryOrderDTO, HttpServletResponse response) throws IOException {
        // 1.查询数据
        List<ListOrderVO> orderList = orderService.exportByDTO(queryOrderDTO);
        // 2.设置响应的数据类型、响应头，设置字符集编码等
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和EasyExcel没有关系
        String fileName = URLEncoder.encode("订单", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        // 3.调用easyExcel写出的方法(去除表头的样式)
        EasyExcel.write(response.getOutputStream(),ListOrderVO.class).useDefaultStyle(false).sheet().doWrite(orderList);
    }

    @ApiOperation("根据id查询订单")
    @GetMapping("/getDetailById/{id}")
    public Result getDetailById(@PathVariable String id){
        DetailOrderVO detailOrder = orderService.getDetailById(id);
        if(detailOrder != null){
            return toDataResult(detailOrder);
        }
        return error("无法获取订单数据，请稍后重试");
    }
}
